capture log close
log using ReplicationFile_Tables_Figures_Frontiers, replace text

// Master-file to reproduce all figures and tables for "Gendered discrimination against immigrants"
// Author: Johanna Gereke 
// Date last modified: 2020.06.27


version 	15.0
clear 		all

cd "/Users/johanna/Dropbox/Ethnic diversity and prosociality/Project 2_InOutGroup/Writing/Frontiers/data/"
global OUT = "/Users/johanna/Dropbox/Ethnic diversity and prosociality/Project 2_InOutGroup/Writing/Frontiers/graphs_tables/"

***************
* Main Graphs *
***************


* H1: In-group bias
* -----------------

use "cleandata_frontiers.dta", clear

matrix define A = J(3, 3, 0)

xtset player_id tasknum

// Dictator Game
xtreg dec_dg pict_imm, fe cluster(player_id)
lincom pict_imm
matrix A[1,1] = r(estimate)
matrix A[1,2] = r(estimate) + 1.96*r(se)
matrix A[1,3] = r(estimate) - 1.96*r(se)

// Trust Game
xtreg dec_tg pict_imm, fe cluster(player_id)
lincom pict_imm
matrix A[2,1] = r(estimate)
matrix A[2,2] = r(estimate) + 1.96*r(se)
matrix A[2,3] = r(estimate) - 1.96*r(se)

// Split Game
reg dec_split if tag == 1, robust  // the constant tells you whether there is bias
lincom _cons
matrix A[3,1] = r(estimate)
matrix A[3,2] = r(estimate) + 1.96*r(se)
matrix A[3,3] = r(estimate) - 1.96*r(se)

svmat A
gen x = _n in 1 / 3

twoway ///
	(rcap A2 A3 x in 1, msize(0) lwidth(vthick) lcolor("65 106 153") ) ///
	(rcap A2 A3 x in 2, msize(0) lwidth(vthick) lcolor("255 0 0") ) ///
	(rcap A2 A3 x in 3, msize(0) lwidth(vthick) lcolor("0 102 0") ) ///
	(rcap A2 A3 x in 1, msize(0) lwidth(thin) lcolor(white) ) ///
	(rcap A2 A3 x in 2, msize(0) lwidth(thin) lcolor(white) ) ///
	(rcap A2 A3 x in 3, msize(0) lwidth(thin) lcolor(white) ) ///
	(scatter A1 x in 1, mcolor("65 106 153") msym(X)) ///
	(scatter A1 x in 2, mcolor("255 0 0") msym(X)) ///
	(scatter A1 x in 3, mcolor("0 102 0") msym(X)) ///
	, legend(off) ///
	yline(0, lcolor("255 182 193")) ///
	scheme(538) ///
	xscale(range(0.5 3.5)) ///
	xlab(1 "Dictator Game" 2 "Trust Game" 3 "Split Game") ///
	xtitle("") ///
	ylab(-.4(.1).2) ///
	ytitle("Difference in Amount in EUR Sent to Immigrants" "Minus  Amount Sent to Natives", height(9))
graph export "${OUT}H1.png", as(png) replace





* H2: Specific Discrimination against immigrant men
* -------------------------------------------------

use "cleandata_frontiers.dta", clear

xtset player_id tasknum

gen interact = pict_imm * pict_male


matrix define A = J(6, 3, 0)

// Dictator Game
xtreg dec_dg pict_imm pict_male interact, fe cluster(player_id)  // not significant
lincom pict_imm  //female immigrant result
matrix A[1,1] = r(estimate)
matrix A[1,2] = r(estimate) + 1.96*r(se)
matrix A[1,3] = r(estimate) - 1.96*r(se)

lincom pict_imm + interact // male immigrant result
matrix A[2,1] = r(estimate)
matrix A[2,2] = r(estimate) + 1.96*r(se)
matrix A[2,3] = r(estimate) - 1.96*r(se)

// Trust Game
xtreg dec_tg pict_imm pict_male interact, fe cluster(player_id)   // interaction significant at 0.01
lincom pict_imm  //female immigrant result
matrix A[3,1] = r(estimate)
matrix A[3,2] = r(estimate) + 1.96*r(se)
matrix A[3,3] = r(estimate) - 1.96*r(se)

lincom pict_imm + interact // male immigrant result
matrix A[4,1] = r(estimate)
matrix A[4,2] = r(estimate) + 1.96*r(se)
matrix A[4,3] = r(estimate) - 1.96*r(se)


// Split Game
reg dec_split ib2.splitpic2 if tag == 1, robust 
lincom _cons // female immigrant result
matrix A[5,1] = r(estimate)
matrix A[5,2] = r(estimate) + 1.96*r(se)
matrix A[5,3] = r(estimate) - 1.96*r(se)

lincom _cons + 1.splitpic2  //male immigrant result
matrix A[6,1] = r(estimate)
matrix A[6,2] = r(estimate) + 1.96*r(se)
matrix A[6,3] = r(estimate) - 1.96*r(se)

// Graphing
svmat A
gen x = _n in 1 / 2
replace x = _n + 1 in 3 / 4
replace x = _n + 2 in 5 / 6


twoway ///
	(rcap A2 A3 x in 1, msize(0) lcolor("65 106 153") lwidth(thick)) ///
	(rcap A2 A3 x in 2, msize(0) lcolor("65 106 153") lwidth(medthick) lpattern(dash) ) ///
	(rcap A2 A3 x in 3, msize(0) lcolor("255 0 0") lwidth(thick)) ///
	(rcap A2 A3 x in 4, msize(0) lcolor("255 0 0") lwidth(medthick) lpattern(dash)) ///
	(rcap A2 A3 x in 5, msize(0) lcolor("0 102 0") lwidth(thick)) ///
	(rcap A2 A3 x in 6, msize(0) lcolor("0 102 0") lwidth(medthick) lpattern(dash)) ///
	(scatter A1 x in 1, mcolor("65 106 153") msym(O)) ///
	(scatter A1 x in 2, mcolor("65 106 153") msym(D)) ///
	(scatter A1 x in 3, mcolor("255 0 0") msym(O)) ///
	(scatter A1 x in 4, mcolor("255 0 0") msym(D)) ///
	(scatter A1 x in 5, mcolor("0 102 0") msym(O)) ///
	(scatter A1 x in 6, mcolor("0 102 0") msym(D)) ///
	, legend(order(11 "Female" 12 "Male") row(1) ring(0) pos(1)) ///
	yline(0, lcolor("255 182 193")) ///
	scheme(538) ///
	xscale(range(0.5 8.5)) ///
	xlab(1.5 "Dictator Game" 4.5 "Trust Game" 7.5 "Split Game") ///
	xtitle("") ///
	ylab(-.4(.1).2) ///
	ytitle("Difference in Amount in EUR Sent to Immigrant Men" "Minus Amount Sent to Immigrant Women", height(9)) ///
	text(-.1 1.5 "n.s.", place(n)) ///
	text(-.1 4.5 "* *") ///
	text(-.1 7.5 "n.s.", place(n))
graph export "${OUT}H2.png", as(png) replace



********************
* Main TABLES *
********************

* Summary statistics
* ------------------

use "cleandata_frontiers.dta", clear

tab employ3, gen(demploy)
tab educ3, gen(deduc)


label var age "Age"
label var male "Male"
label var  deduc3 "Abitur (university-qualifying high school degree)"
label var  deduc2 "Realschule or Fachhochschulreife"
label var  deduc1 "Hauptschule or lower"
label var demploy1 "Full-time employed"
label var demploy2  "Part-time employed"
label var demploy3 	"Other status"
label var CDU "CDU"
label var SPD "SPD"
label var LINKE  "LINKE"
label var  AfD "AfD"
label var GREENS "GREENS"
label var rightwing1 "Foreigners only exploit the welfare state"
label var rightwing2 "Only Germans should receive child support"
label var dec_split_imm "Split Game"
label var dec_dg "Dictator Game"
label var dec_tg "Trust Game"


sutex age male deduc* demploy* CDU SPD LINKE AfD GREENS rightwing* dec_split_imm if tag == 1, minmax labels
sutex  dec_dg  dec_tg 

* Dictator Game
* -------------

use "cleandata_frontiers.dta", clear

xtset player_id tasknum

estimates clear
eststo: xtreg dec_dg i.pict_imm, fe cluster(player_id)
estadd local fes "Yes"
eststo: xtreg dec_dg i.pict_imm i.tasknum, fe cluster(player_id)
estadd local fes "Yes"
eststo: xtreg dec_dg i.pict_imm i.pict_male, fe cluster(player_id)
estadd local fes "Yes"
eststo: xtreg dec_dg i.pict_imm##i.pict_male, fe cluster(player_id)
estadd local fes "Yes"
eststo: xtreg dec_dg i.pict_imm##i.pict_male i.tasknum, fe cluster(player_id)
estadd local fes "Yes"

esttab est* 
esttab est* using "${OUT}DG_table.tex", replace ///
	b(%8.3f) se(%8.3f) ///
	scalars("fes Respondent Fixed Effects") ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	order(1.pict_imm 1.pict_male 1.pict_imm#1.pict_male 2.tasknum _cons) ///
	keep(1.pict_imm 1.pict_male 1.pict_imm#1.pict_male 2.tasknum _cons) ///
	coeflabels(1.pict_imm "Immigrant Alter" 1.pict_male "Male Alter" 1.pict_imm#1.pict_male "Immigrant $\times$ Male" 2.tasknum "Second Decision" _cons "Constant") ///
	nodepvars nomtitles nonotes ///
	nolines posthead(\midrule) prefoot(\midrule) postfoot(\bottomrule \end{tabular}}) ///
	tex compress	

	
* Trust Game
* -----------

use "cleandata_frontiers.dta", clear

xtset player_id tasknum

estimates clear
eststo: xtreg dec_tg i.pict_imm, fe cluster(player_id)
estadd local fes "Yes"
eststo: xtreg dec_tg i.pict_imm i.tasknum, fe cluster(player_id)
estadd local fes "Yes"
eststo: xtreg dec_tg i.pict_imm i.pict_male, fe cluster(player_id)
estadd local fes "Yes"
eststo: xtreg dec_tg i.pict_imm##i.pict_male, fe cluster(player_id)
estadd local fes "Yes"
eststo: xtreg dec_tg i.pict_imm##i.pict_male i.tasknum, fe cluster(player_id)
estadd local fes "Yes"

esttab est* 
esttab est* using "${OUT}TG_table.tex", replace ///
	b(%8.3f) se(%8.3f) ///
	scalars("fes Respondent Fixed Effects") ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	order(1.pict_imm 1.pict_male 1.pict_imm#1.pict_male 2.tasknum _cons) ///
	keep(1.pict_imm 1.pict_male 1.pict_imm#1.pict_male 2.tasknum _cons) ///
	coeflabels(1.pict_imm "Immigrant Alter" 1.pict_male "Male Alter" 1.pict_imm#1.pict_male "Immigrant $\times$ Male" 2.tasknum "Second Decision" _cons "Constant") ///
	nodepvars nomtitles nonotes ///
	nolines posthead(\midrule) prefoot(\midrule) postfoot(\bottomrule \end{tabular}}) ///
	tex compress	


	
* Split Game
* ---------

use "cleandata_frontiers.dta", clear
keep if tag == 1


estimates clear
eststo: reg dec_split , robust  
eststo: reg dec_split ib2.splitpic2, robust  

esttab est* 
esttab est* using "${OUT}Split_Table.tex", replace ///
	b(%8.3f) se(%8.3f) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	coeflabels(1.splitpic2 "Male Alters" _cons "Constant") ///
	nodepvars nomtitles nonotes ///
	nolines posthead(\midrule) prefoot(\midrule) postfoot(\bottomrule \end{tabular}}) ///
	tex compress	

	
**************************************	
**************************************
* Appendix Tables *
**************************************
**************************************

use "cleandata_frontiers.dta", clear

* Standardization of DVs

* Dictator Game 

foreach var of varlist dec_dg dec_tg dec_split_imm {
	egen std`var' = std(`var')	
}

xtset player_id tasknum

estimates clear
eststo: xtreg stddec_dg i.pict_imm, fe cluster(player_id)
estadd local fes "Yes"
eststo: xtreg stddec_dg i.pict_imm i.tasknum, fe cluster(player_id)
estadd local fes "Yes"
eststo: xtreg stddec_dg i.pict_imm i.pict_male, fe cluster(player_id)
estadd local fes "Yes"
eststo: xtreg stddec_dg i.pict_imm##i.pict_male, fe cluster(player_id)
estadd local fes "Yes"
eststo: xtreg stddec_dg i.pict_imm##i.pict_male i.tasknum, fe cluster(player_id)
estadd local fes "Yes"

esttab est* 
esttab est* using "${OUT}DG_table_Std_Appendix.tex", replace ///
	b(%8.3f) se(%8.3f) ///
	scalars("fes Respondent Fixed Effects") ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	order(1.pict_imm 1.pict_male 1.pict_imm#1.pict_male 2.tasknum _cons) ///
	keep(1.pict_imm 1.pict_male 1.pict_imm#1.pict_male 2.tasknum _cons) ///
	coeflabels(1.pict_imm "Immigrant Alter" 1.pict_male "Male Alter" 1.pict_imm#1.pict_male "Immigrant $\times$ Male" 2.tasknum "Second Decision" _cons "Constant") ///
	nodepvars nomtitles nonotes ///
	nolines posthead(\midrule) prefoot(\midrule) postfoot(\bottomrule \end{tabular}}) ///
	tex compress	


* Trust Game
* -----------

xtset player_id tasknum

estimates clear
eststo: xtreg stddec_tg i.pict_imm, fe cluster(player_id)
estadd local fes "Yes"
eststo: xtreg stddec_tg i.pict_imm i.tasknum, fe cluster(player_id)
estadd local fes "Yes"
eststo: xtreg stddec_tg i.pict_imm i.pict_male, fe cluster(player_id)
estadd local fes "Yes"
eststo: xtreg stddec_tg i.pict_imm##i.pict_male, fe cluster(player_id)
estadd local fes "Yes"
eststo: xtreg stddec_tg i.pict_imm##i.pict_male i.tasknum, fe cluster(player_id)
estadd local fes "Yes"

esttab est* 
esttab est* using "${OUT}TG_table_Std_Appendix.tex", replace ///
	b(%8.3f) se(%8.3f) ///
	scalars("fes Respondent Fixed Effects") ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	order(1.pict_imm 1.pict_male 1.pict_imm#1.pict_male 2.tasknum _cons) ///
	keep(1.pict_imm 1.pict_male 1.pict_imm#1.pict_male 2.tasknum _cons) ///
	coeflabels(1.pict_imm "Immigrant Alter" 1.pict_male "Male Alter" 1.pict_imm#1.pict_male "Immigrant $\times$ Male" 2.tasknum "Second Decision" _cons "Constant") ///
	nodepvars nomtitles nonotes ///
	nolines posthead(\midrule) prefoot(\midrule) postfoot(\bottomrule \end{tabular}}) ///
	tex compress	
	

* Split Game
* ---------

use "cleandata_frontiers.dta", clear


foreach var of varlist dec_dg dec_tg dec_split_imm {
	egen std`var' = std(`var')	
}

keep if tag == 1

* models 
estimates clear
eststo: reg stddec_split, robust 
eststo: reg stddec_split ib2.splitpic2, robust  

esttab est* 
esttab est* using "${OUT}Split_Table_Std_Appendix.tex", replace ///
	b(%8.3f) se(%8.3f) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	coeflabels(1.splitpic2 "Male Alters" _cons "Constant") ///
	nodepvars nomtitles nonotes ///
	nolines posthead(\midrule) prefoot(\midrule) postfoot(\bottomrule \end{tabular}}) ///
	tex compress	

	
	
	
	
******************************
* Interaction Terms 
******************************	


* Marginal Effects for Interaction Terms in  DG & TG (Table 2 and 3 in Main Manuscript) 
**************************************************************************************	
	

* DG Average Marginal Effects to test significance of interaction term (not shown in manuscript)
xtset player_id tasknum

estimates clear
xtreg dec_dg i.pict_imm##i.pict_male i.tasknum, fe cluster(player_id) 
eststo: margins  pict_imm#pict_male, coeflegend post

esttab est*,  
esttab est* using "${OUT}DG_table_margins.tex", replace ///
b(%8.3f) se(%8.3f) ///
nostar nolines posthead(\midrule) prefoot(\midrule) postfoot(\bottomrule \end{tabular}}) ///	
tex compress
pwcompare i(1)pict_male#pict_imm, effect


* TG Average Marginal Effects to test significance of interaction term 
xtset player_id tasknum

estimates clear
xtreg dec_tg i.pict_imm##i.pict_male i.tasknum, fe cluster(player_id)
eststo: margins pict_imm#pict_male, coeflegend post

esttab est* 
esttab est* using "${OUT}TG_table_margins.tex", replace ///
b(%8.3f) se(%8.3f) ///
nostar nolines posthead(\midrule) prefoot(\midrule) postfoot(\bottomrule \end{tabular}}) ///	
tex compress
pwcompare i(1)pict_male#pict_imm, effect




* 3 way interaction:  TG with FE
******************************		
use "cleandata_frontiers.dta", clear

* Defining labels
label define genderlab2 0 "Female Ego" 1 "Male Ego"
label values male genderlab2
label define pictgenderlab 0 "female alter" 1 "male alter"
label values pict_male pictgenderlab
label define immlab1 0 "native alter" 1 "immigrant alter"
label values pict_imm immlab1


* setting up interaction terms
* -----------------------------

gen inter_male_pictimm = male * pict_imm
gen inter_male_pictmale = male * pict_male
gen inter_pict_imm_male = pict_male * pict_imm
gen inter3 = male * pict_imm * pict_male


* FE models
xtset player_id 

estimates clear
eststo: xtreg dec_tg pict_imm pict_male inter_pict_imm_male, fe cluster(player_id)
estadd local fes "Yes"
eststo: xtreg dec_tg pict_imm inter_male_pictimm, fe cluster(player_id)
estadd local fes "Yes"
eststo: xtreg dec_tg pict_male inter_male_pictmale, fe cluster(player_id)
estadd local fes "Yes"
eststo: xtreg dec_tg pict_imm pict_male inter_pict_imm_male inter_male_pictimm inter_male_pictmale inter3, fe cluster(player_id)
estadd local fes "Yes"

esttab est* 
esttab est* using "${OUT}TG_Appendix_table_FE.tex", replace ///
	b(%8.3f) se(%8.3f) ///
	scalars("fes Respondent Fixed Effects") ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	order(pict_imm pict_male inter_pict_imm_male inter_male_pictimm inter_male_pictmale inter3 _cons) ///
	keep(pict_imm pict_male inter_pict_imm_male inter_male_pictimm inter_male_pictmale inter3 _cons) ///
	coeflabels(pict_imm "Immigrant Alter" pict_male "Male Alter" inter_pict_imm_male "Immigrant $\times$ Male Alter" ///
	inter_male_pictimm "Male Ego $\times$ Immigrant Alter"  inter_male_pictmale "Male Ego $\times$ Male Alter" ///
	inter3 "Immigrant Alter $\times$ Male Alter $\times$ Male Ego" _cons "Constant") ///
	nodepvars nomtitles nonotes ///
	nolines posthead(\midrule) prefoot(\midrule) postfoot(\bottomrule \end{tabular}}) ///
	tex compress	
	
	

******************************
* 3 way interaction:  TG with RE
******************************	
	

use "cleandata_frontiers.dta", clear

* Defining labels
label define genderlab2 0 "Female Ego" 1 "Male Ego"
label values male genderlab2
label define pictgenderlab 0 "female alter" 1 "male alter"
label values pict_male pictgenderlab
label define immlab1 0 "native alter" 1 "immigrant alter"
label values pict_imm immlab1


* setting up interaction terms
* -----------------------------

gen inter_male_pictimm = male * pict_imm
gen inter_male_pictmale = male * pict_male
gen inter_pict_imm_male = pict_male * pict_imm
gen inter3 = male * pict_imm * pict_male

	
* RE models
xtset player_id 

estimates clear
eststo: xtreg dec_tg pict_imm pict_male inter_pict_imm_male, cluster(player_id)
eststo: xtreg dec_tg pict_imm male inter_male_pictimm, cluster(player_id)
eststo: xtreg dec_tg pict_male male inter_male_pictmale, cluster(player_id)
eststo: xtreg dec_tg pict_imm pict_male male inter_pict_imm_male inter_male_pictimm inter_male_pictmale inter3, cluster(player_id)


esttab est* 
esttab est* using "${OUT}TG_Appendix_table_RE.tex", replace ///
	b(%8.3f) se(%8.3f) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	order(pict_imm male pict_male inter_pict_imm_male inter_male_pictimm inter_male_pictmale inter3 _cons) ///
	keep(pict_imm male pict_male inter_pict_imm_male inter_male_pictimm inter_male_pictmale inter3 _cons) ///
	coeflabels(pict_imm "Immigrant Alter" male "Male Ego" pict_male "Male Alter" inter_pict_imm_male "Immigrant $\times$ Male Alter" ///
	inter_male_pictimm "Male Ego $\times$ Immigrant Alter"  inter_male_pictmale "Male Ego $\times$ Male Alter" ///
	inter3 "Immigrant Alter $\times$ Male Alter $\times$ Male Ego" _cons "Constant") ///
	nodepvars nomtitles nonotes ///
	nolines posthead(\midrule) prefoot(\midrule) postfoot(\bottomrule \end{tabular}}) ///
	tex compress
		
			

* plotting margins for 3 way interaction
* -----------------------------

xi: xtreg dec_tg pict_imm##pict_male##male, cluster(player_id)
margins male#pict_imm#pict_male
marginsplot, by(male) x(pict_male) noci xtitle(Profile of Alter) xlabel(, angle(45))
graph export "${OUT}TG_3way_margins.png", as(png) replace
pwcompare i(1)pict_male#i(1).pict_imm#male,  effect



log close
exit


